package br.com.academia.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.academia.beans.Aluno;

public class AlunoDAO {
	private Connection connection;
	
	public AlunoDAO(Connection connection) {
		this.connection = connection;
	}
	
	public List<String[]> consultaAluno(List<String> dadosConsulta)  {
		List<String[]> retorno = new ArrayList<String[]>();
		String query = "Select * from Alunos";
		 try {
			 PreparedStatement stm = connection.prepareStatement(query);
			 ResultSet rs = stm.executeQuery();
			 while(rs.next()) {
				 String[] row = new String[dadosConsulta.size()];
				 for(int i = 0; i < row.length; i++) {
					 row[i] = rs.getString(dadosConsulta.get(i));
				 }
				 retorno.add(row);
			 }
		 } catch(SQLException e) {
			 e.printStackTrace();
		 }
		return retorno;
	}
	
	public Aluno consultaAlunoId(int idAluno) {
		Aluno aluno = new Aluno();
		String query = "Select * from Alunos where idAluno = ?";
		try {
			PreparedStatement stm = connection.prepareStatement(query);
			stm.setInt(1, idAluno);
			ResultSet rs = stm.executeQuery();
			
			if(rs.next()) {
				aluno.setIdAluno(rs.getInt("idAluno"));
				aluno.setFilial_idFilial(rs.getInt("Filial_idFilial"));
				aluno.setNome(rs.getString("nome"));
				aluno.setSobreNome(rs.getString("sobreNome"));
				aluno.setEndereco(rs.getString("endereco"));
				aluno.setNumero(rs.getInt("numero"));
				aluno.setRg(Integer.parseInt(rs.getString("rg")));
				aluno.setCpf(rs.getString("cpf"));
				aluno.setBairro(rs.getString("bairro"));
				aluno.setCep(rs.getInt("cep"));
				aluno.setNumeroApartamento(rs.getInt("numeroApartamento"));
				aluno.setLoginUsuario(rs.getString("loginUsuario"));
				aluno.setSenhaUsuario(rs.getString("senhaUsuario"));
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		
		return aluno;
	}
	
	public List<Aluno> buscaAlunoSelecao() {
		List<Aluno> listaAluno = new ArrayList<Aluno>();
		String query = "Select idAluno, nome from Alunos";

		try {
			PreparedStatement stm = connection.prepareStatement(query);
			ResultSet rs = stm.executeQuery();

			while (rs.next()) {
				Aluno aluno = new Aluno();
				aluno.setIdAluno(rs.getInt("idAluno"));
				aluno.setNome(rs.getString("nome"));
				listaAluno.add(aluno);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return listaAluno;
	}
	
	public Aluno consultaAlunoIdTreino(int idTreino) {
		Aluno aluno = new Aluno();
		String query = "select alunos.nome, alunos.idAluno from ALunos " +
				" left Join Treinos on Treinos.Aluno_idAluno = Alunos.idAluno " +
				" where Treinos.idTreino = ?";

		try {
			PreparedStatement stm = connection.prepareStatement(query);
			stm.setInt(1, idTreino);
			ResultSet rs = stm.executeQuery();

			if (rs.next()) {
				aluno.setIdAluno(rs.getInt("idAluno"));
				aluno.setNome(rs.getString("nome"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return aluno;
	}
}

